home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / clients / editres / actions.c next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  8.9 KB  |  337 lines

  1. /*
  2.  * $XConsortium: actions.c,v 1.12 91/07/09 12:09:07 rws Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #ifdef MSDOS
  26. #include "X11/Intrinsc.h"      /* QDK 05/11/1994 12:54pm. */
  27. #else
  28. #include "X11/Intrinsic.h"
  29. #endif
  30. #include <X11/Xutil.h>
  31.  
  32. #include <X11/Xaw/Cardinals.h>    
  33.  
  34. #include "editresP.h"
  35.  
  36. /*
  37.  * External Functions.
  38.  */
  39.  
  40. extern void SetMessage(), _TreeSelect(), _TreeSelectNode(), _FindWidget();
  41. extern void _TreeActivateNode(), _TreeRelabel(), _TreeRelabelNode();
  42. extern void PrepareToLayoutTree(), LayoutTree(), _PopdownFileDialog();
  43.  
  44. /*
  45.  * Private data.
  46.  */
  47.  
  48. struct ActionValues {
  49.     String name;
  50.     int type;
  51. };
  52.  
  53. static struct ActionValues select_values[] = {
  54.     { "widget", (int) SelectWidget },
  55.     { "all", (int) SelectAll },
  56.     { "nothing", (int) SelectNone },
  57.     { "invert", (int) SelectInvert },
  58.     { "children", (int) SelectChildren },
  59.     { "descendants", (int) SelectDescendants },
  60.     { "parent", (int) SelectParent },
  61.     { "ancestors", (int) SelectAncestors }
  62. };
  63.  
  64. static struct  ActionValues label_values[] = {
  65.     { "name", (int) NameLabel },
  66.     { "class", (int) ClassLabel },
  67.     { "id", (int) IDLabel },
  68.     { "window", (int) WindowLabel },
  69.     { "toggle", (int) ToggleLabel }
  70. };
  71.  
  72. static WNode * FindTreeNodeFromWidget();
  73. static Boolean CheckAndFindEntry();
  74.  
  75. /*    Function Name: SelectAction
  76.  *    Description: 
  77.  *      Arguments: w - any widget in the widget tree.
  78.  *                 event - NOT USED.
  79.  *                 params, num_params - the parameters paseed to the action
  80.  *                                      routine. 
  81.  *
  82.  * params[0] - One of "nothing", "parent", "children", "ancestors",
  83.  *                    "descendants", "invert", "all"
  84.  * num_params - must be one.
  85.  */
  86.  
  87. /* ARGSUSED */
  88. static void
  89. SelectAction(w, event, params, num_params)
  90. Widget w;
  91. XEvent * event;
  92. String * params;
  93. Cardinal * num_params;
  94. {
  95.     WNode * node;
  96.     int type;
  97.  
  98.     if (!CheckAndFindEntry("Select", params, *num_params, 
  99.                select_values, XtNumber(select_values), &type))
  100.     return;
  101.  
  102.     switch(type) {
  103.     case SelectAll:
  104.     case SelectNone:
  105.     case SelectInvert:
  106.     _TreeSelect(global_tree_info, type);
  107.     break;
  108.     case SelectWidget:
  109.     _FindWidget(XtParent(w));
  110.     break;
  111.     default:
  112.     node = FindTreeNodeFromWidget(w);
  113.     if (node)
  114.         _TreeActivateNode(node, type);    
  115.     else
  116.         _TreeSelect(global_tree_info, type);
  117.     break;
  118.     }
  119. }
  120.  
  121. /*    Function Name: RelabelAction
  122.  *    Description: 
  123.  *      Arguments: w - any widget in the widget tree.
  124.  *                 event - NOT USED.
  125.  *                 params, num_params - the parameters paseed to the action
  126.  *                                      routine. 
  127.  *
  128.  * params[0] - One of "name", "class", "id"
  129.  * num_params - must be one.
  130.  */
  131.  
  132. /* ARGSUSED */
  133. static void
  134. RelabelAction(w, event, params, num_params)
  135. Widget w;
  136. XEvent * event;
  137. String * params;
  138. Cardinal * num_params;
  139. {
  140.     WNode * node;
  141.     int type;
  142.  
  143.     if (!CheckAndFindEntry("Relabel", params, *num_params, 
  144.                label_values, XtNumber(label_values), &type))
  145.     return;
  146.  
  147.     if ((node = FindTreeNodeFromWidget(w)) == NULL) 
  148.     _TreeRelabel(global_tree_info, type);
  149.     else {
  150.     PrepareToLayoutTree(global_tree_info->tree_widget); 
  151.     _TreeRelabelNode(node, type, FALSE);
  152.     LayoutTree(global_tree_info->tree_widget); 
  153.     }
  154. }
  155.  
  156. /*    Function Name: PopdownFileDialogAction
  157.  *    Description: Pops down the file dialog widget.
  158.  *                   and calls the approipriate handler.
  159.  *    Arguments: w - any child of the dialog widget.
  160.  *                 event - the event that caused this action.
  161.  *                 params, num_params - params passed to the action routine.
  162.  * RETURNED        none.
  163.  */
  164.  
  165. /* ARGSUSED */
  166.  
  167. static void
  168. PopdownFileDialogAction(w, event, params, num_params)
  169. Widget w;
  170. XEvent * event;
  171. String * params;
  172. Cardinal * num_params;
  173. {
  174.     char buf[BUFSIZ];
  175.     Boolean val;
  176.  
  177.     if (*num_params != 1) {
  178.     sprintf(buf, "Action `%s' must have exactly one argument.", 
  179.         "PopdownFileDialog");
  180.  
  181.     SetMessage(global_screen_data.info_label, buf);
  182.     return;
  183.     }
  184.  
  185.     XmuCopyISOLatin1Lowered(buf, params[0]);
  186.  
  187.     if (streq(buf, "cancel"))
  188.     val = FALSE;
  189.     else if (streq(buf, "okay"))
  190.     val = TRUE;
  191.     else {
  192.     sprintf(buf, "Action %s's argument must be either `cancal' or `okay'.",
  193.         "PopdownFileDialog");
  194.  
  195.     SetMessage(global_screen_data.info_label, buf);
  196.     return;
  197.     }
  198.  
  199.     _PopdownFileDialog(w, (XtPointer) val, NULL);
  200. }
  201.  
  202. /*    Function Name: ActionQuit
  203.  *    Description: This function prints a message to stdout.
  204.  *    Arguments: w - ** UNUSED **
  205.  *                 call_data - ** UNUSED **
  206.  *                 client_data - ** UNUSED **
  207.  *    Returns: none
  208.  */
  209.  
  210. extern Widget toplevel;
  211. /* ARGSUSED */
  212. static void
  213. ActionQuit(w, event, params, num_params)
  214. Widget w;
  215. XEvent * event;
  216. String * params;
  217. Cardinal * num_params;
  218. {
  219.   if (w==toplevel) {
  220.     XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
  221.     exit(0);
  222.   }
  223.   else  {
  224.     if (streq(XtName(w), RESOURCE_BOX))
  225.       global_resource_box_up = FALSE;
  226.     XtPopdown(w);
  227.     XtDestroyWidget(w);
  228.   }
  229. }
  230.  
  231. /*    Function Name: SetApplicationActions
  232.  *    Description: Sets my application actions.
  233.  *    Arguments: app_con - the application context.
  234.  *    Returns: none.
  235.  */
  236.  
  237. extern void ModifySVEntry();
  238.  
  239. static XtActionsRec actions[] = {
  240.   {"Select",            SelectAction},
  241.   {"SVActiveEntry",     ModifySVEntry},
  242.   {"Relabel",          RelabelAction}, 
  243.   {"PopdownFileDialog", PopdownFileDialogAction},
  244.   {"quit",              ActionQuit}
  245. };
  246.  
  247. void
  248. SetApplicationActions(app_con)
  249. XtAppContext app_con;
  250. {
  251.     XtAppAddActions(app_con, actions, XtNumber(actions));
  252.     
  253. }
  254.  
  255.  
  256. /************************************************************
  257.  *
  258.  * Private functions    
  259.  *
  260.  ************************************************************/
  261.  
  262. /*    Function Name: CheckAndFindEntry
  263.  *    Description: Checks the args to make sure they are valid,
  264.  *                   then parses the arg list to find the correct action
  265.  *                   to take.
  266.  *    Arguments: action_name - name of the action (for error messages).
  267.  *                 params, num_params - params passed to the action routine.
  268.  *                 table, num_table - table to check the parameters against.
  269.  * RETURNED        type - info about the action to take.
  270.  *    Returns: TRUE if the arguments are okay.
  271.  */
  272.  
  273. static Boolean
  274. CheckAndFindEntry(action_name, params, num_params, table, num_table, type)
  275. String * params, action_name;
  276. Cardinal num_params, num_table;
  277. struct ActionValues * table;
  278. int * type;
  279. {
  280.     char buf[BUFSIZ];
  281.     int i;
  282.  
  283.     if (num_params != 1) {
  284.     sprintf(buf, "Action `%s' must have exactly one argument.", 
  285.         action_name);
  286.     SetMessage(global_screen_data.info_label, buf);
  287.     return(FALSE);
  288.     }
  289.     
  290.     XmuCopyISOLatin1Lowered(buf, params[0]);
  291.     for ( i = 0 ; i < num_table; i++ ) 
  292.     if (streq(buf, table[i].name)) {
  293.         *type = table[i].type;
  294.         return(TRUE);
  295.     }
  296.     
  297.     sprintf(buf,"Unknown parameter to action `%s' must be one of:\n", 
  298.         action_name);
  299.  
  300.     for (i = 0; i < num_table; ) {
  301.     strcat(buf, table[i++].name);
  302.     
  303.     if (i == (num_table - 1))
  304.         strcat(buf, ", or ");
  305.     else if (i < num_table)
  306.         strcat(buf, ", ");
  307.     }
  308.     
  309.     SetMessage(global_screen_data.info_label, buf);
  310.     return(FALSE);
  311. }
  312.  
  313. /*    Function Name: FindTreeNodeFromWidget
  314.  *    Description: finds the tree node associated with a widget.
  315.  *    Arguments: w - widget to check.
  316.  *    Returns: the node associated with this widget, or NULL.
  317.  */
  318.  
  319. static WNode *
  320. FindTreeNodeFromWidget(w)
  321. Widget w;
  322. {
  323.     int ret_val;
  324.     caddr_t data_return;
  325.  
  326.     /*
  327.      * Yes, I really am casting a widget to a window  ** TRUST ME ***
  328.      */
  329.  
  330.     ret_val = XFindContext(XtDisplay(w), (Window) w, NODE_INFO, &data_return);
  331.  
  332.     if (ret_val == 0) 
  333.     return((WNode *) data_return);
  334.     return(NULL);
  335. }
  336.  
  337.